<template>
	<view class="h-fill">
		<u-navbar title="我的邀请码" titleColor="#fff" backIconColor="#fff" :background="navBg"></u-navbar>
		<view class="code-content uni-flex uni-column align-center justify-center">
			<view class="qrcode">
				<image class="h-fill v-fill" :src="qrcodeUrl"></image>
			</view>
			<view class="btn-save t-mg-2">
				<navigator url="share-poster-list">
					<nice-red-button text="分享海报"></nice-red-button>
				</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	import {getMyCode} from "../../api/platform/shareExpertCode.js"
	import uQRCode from '@/js_sdk/Sansnn-uQRCode/uqrcode.js'
	
	const canvasW = uni.upx2px(597)
	const canvasH = uni.upx2px(832)
	export default {
		data() {
			return {
				navBg: {
					'background-image': 'linear-gradient(to right, #434AD8, #8E77F5)'
				},
				code: {},
				ready: false,
				qrcodeUrl: null
			}
		},
		onReady() {
			this.ctx = uni.createCanvasContext('canvas', this)
			this.ctx.setFillStyle('#F5F5F5')
			this.ctx.fillRect(0, 0, this.canvasW, this.canvasH)
			this.loadData();
		},
		methods: {
			getImageInfo(imgSrc){
				return new Promise((resolve, reject) => {
					uni.getImageInfo({
						src: imgSrc,
						success: (image) => {
							resolve(image);
							console.log('获取图片成功',image)
						},
						fail: (err) => {
							reject(err);
							console.log('获取图片失败',err)
						}
					});
				});
			},
			//保存图片到相册
			async saveImage(){
				try{
					await new Promise((resolve, reject) => {
						uni.getSetting({
							success: (res) => {
								let t = res.authSetting["scope.writePhotosAlbum"];
								if (t !== undefined && !t) {
									// 请求过权限，但是用户关闭了
									uni.showModal({
										title: "请先打开相册开关",
										cancelText: "取消",
										confirmText: "去打开",
										success: (res) => {
											if (res.confirm) {
												uni.openSetting();
											}
										}
									})
									rejcet();
								} else {
									resolve();
								}
							}
						})
					})
				}catch(e){
					return;
				}
				var that = this
				uni.canvasToTempFilePath({
					canvasId: 'canvas',
					quality: 1,
					complete: (res) => {
						console.log('保存到相册',res);
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success(res) {
								uni.showToast({
									title: '已保存到相册',
									icon: 'success',
									duration: 2000
								})
								setTimeout(()=>{
									that.isShow = false
								},2000)
							}
						})
					}
				}, this);
			},
			async loadData() {
				try{
					uni.showLoading({
						mask: true
					})
					this.code = await getMyCode({
						showLoading: false
					});
					let text = encodeURIComponent("https://test.the3dspace.cn/register-share-expert?codeId=" + this.code._id);
					this.qrcodeUrl = `${this.$cloudUrl}/genQRCode?text=${text}`
					uni.hideLoading()
				}catch(e){
					this.$log("加载我的邀请码遇到错误", e);
					uni.hideLoading();
				}
			}
		}
	}
</script>

<style>
	.qrcode {
		width: 400rpx;
		height: 400rpx;
	}
	.btn-save {
		width: 100%;
	}
	.code-content {
		height: 100vh;
		position: absolute;
		width: 100%;
		top: 0;
	}
</style>
